package com.goldsprite.microhorigame.screens.games;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.viewport.*;
import com.goldsprite.gameframeworks.assets.*;
import com.goldsprite.gameframeworks.ecs.*;
import com.goldsprite.gameframeworks.screens.*;

public class StartMenuScreen extends GScreen {
	private OrthographicCamera worldCam, uiCam;
	private Stage stage;
	private SpriteBatch batch;
	private TextureRegion skyRegion;

	private GameRes res = GameRes.Background_Forest;
	private Array<TextureRegion> regions;
	private TextureRegion titleRegion;
	private TextButton.TextButtonStyle btnStyle;
	private FitViewport stageViewport;

	@Override
	public void create() {
		uiCam = getCamera();
		// 初始化世界相机
		worldCam = new OrthographicCamera();
		worldCam.setToOrtho(false, uiCam.viewportWidth, uiCam.viewportHeight);

		batch = new SpriteBatch();

		//背景
		regions = SpriteUtils.splitAllFrames(res, 1, 6, 6);
		skyRegion = regions.get(0);
		skyRegion.getTexture().setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.ClampToEdge);
		baseTextureWidth = skyRegion.getRegionWidth();
		baseTextureHeight = skyRegion.getRegionHeight();

		//title
		Texture titleTex = GameRes.StartTitle.getResource();
		titleTex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
		titleRegion = new TextureRegion(titleTex);

		//ui
		{
			float viewportAspectRatio = getViewSize().x/getViewSize().y;
			float stageHeight = 256;
			float stageWidth = 256 * viewportAspectRatio;
			stageViewport = new FitViewport(stageWidth, stageHeight);
			stage = new Stage(stageViewport);

			getImp().addProcessor(stage);

			Texture btnUpTex = GameRes.Btn_Up.getResource();
			Texture btnDownTex = GameRes.Btn_Down.getResource();

			int k4;
			int[] edge = GameRes.Btn_Up.getNinePatchEdge();
			NinePatchDrawable btnUpNinePatch = new NinePatchDrawable(new NinePatch(btnUpTex, edge[0], edge[1], edge[2], edge[3]));

			edge = GameRes.Btn_Down.getNinePatchEdge();
			NinePatchDrawable btnDownNinePatch = new NinePatchDrawable(new NinePatch(btnDownTex, edge[0], edge[1], edge[2], edge[3]));

			btnStyle = new TextButton.TextButtonStyle();
			btnStyle.up = btnUpNinePatch;
			btnStyle.down = btnDownNinePatch;
			btnStyle.font = GameFontUtils.generateAutoClarity(10);

			//Skin editorSkin = GlobalAssets.getInstance().editorSkin;
			TextButton startBtn = new TextButton("Start", btnStyle);
			startBtn.setSize(100, 28);
			startBtn.setPosition(stageWidth/2f, stageHeight/2f-stageHeight*0.1f, Align.center);
			startBtn.addListener(new ClickListener(){
				public void clicked(InputEvent e, float x, float y) {
					getScreenManager().setCurScreen(MainGameScreen.class, true);
				}
			});
			stage.addActor(startBtn);
		}
	}

	@Override
	public void render0(float delta) {
		batch.setProjectionMatrix(worldCam.combined);
		batch.begin();

		//背景
		renderRollingSky(batch, regions.get(0), 0.12f, rollX*0.15f);
		renderRollingSky(batch, regions.get(1), 0.12f, rollX*0.2f);
		renderRollingSky(batch, regions.get(2), 0.12f, rollX*0.32f);
		renderRollingSky(batch, regions.get(3), 0.12f, rollX*0.5f);
		renderRollingSky(batch, regions.get(4), 0.12f, rollX*0.6f);
		renderRollingSky(batch, regions.get(5), 0.00f, 0);
		//rolling
		float rollVel = 0.03f;//0.5为1秒半张
		rollX += delta * rollVel;

		//titleLogo
		float titleX = getViewCenter().x;
		float titleY = getViewSize().y * (1-0.28f);
		float scl = 0.25f;
		float titleWidth = titleRegion.getRegionWidth() * scl;
		float titleHeight = titleRegion.getRegionHeight() * scl;
		batch.draw(titleRegion, titleX-titleWidth/2f, titleY-titleHeight/2f, titleWidth, titleHeight);

		batch.end();

		stage.act(delta);
		stage.draw();
	}

	float rollX = 0;
	private float[] baseUV = new float[4];
	private float baseTextureWidth, baseTextureHeight; // 基础纹理尺寸
	private void renderRollingSky(SpriteBatch batch, TextureRegion region, float offsetY, float rollX) {// 获取视口尺寸
		float viewportWidth = getViewSize().x;
		float viewportHeight = getViewSize().y;
		float textureAspectRatio = baseTextureWidth / baseTextureHeight;
		int renderSizeY = (int)viewportHeight;  // 高度占满
		int renderSizeX = (int)(viewportHeight * textureAspectRatio);  // 宽度按比例计算

		if(rollX == 0) {
			batch.draw(region, viewportWidth-renderSizeX, viewportHeight-renderSizeY, renderSizeX, renderSizeY);
			return;
		}

		baseUV[0] = region.getU();
		baseUV[1] = region.getV();
		baseUV[2] = region.getU2();
		baseUV[3] = region.getV2();
		float uvWidth = baseUV[2] - baseUV[0];
		float uvHeight = baseUV[3] - baseUV[1];
		float viewportAspect = viewportWidth / viewportHeight;
		float textureAspect = baseTextureWidth / baseTextureHeight;
		float scale = textureAspect / viewportAspect;
		float uvOffset = (1.0f - scale) * 0.5f * uvWidth;

		offsetY *= baseUV[3]-baseUV[1];
		region.setRegion(baseUV[0]-uvOffset+rollX, baseUV[1]+offsetY, baseUV[2]+uvOffset+rollX, baseUV[3]);

		batch.draw(region, 0, 0, viewportWidth, viewportHeight);

		region.setRegion(baseUV[0],  baseUV[1], baseUV[2], baseUV[3]);
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);

		stageViewport.update(width, height, true);
	}
}
